package com.yeyks.app.controller.base.activity;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.WeightRandom;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yeyks.app.annotation.UnUpdateCheck;
import com.yeyks.common.ajax.AjaxResponseBody;
import com.yeyks.common.utils.base.PhoneUtil;
import com.yeyks.commonReference.service.consumer.CashOutOrderService;
import com.yeyks.commonReference.service.order.OrderHeadService;
import com.yeyks.consumer.dal.domain.CashOutOrder;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.*;

@Api(tags = "推手", description = "推手")
@Slf4j
@RestController
@RequestMapping(value = "/app/activity/user/maker")
public class ActivityUserMakerController {

    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Autowired
    private CashOutOrderService cashOutOrderService;
    @Autowired
    private OrderHeadService orderHeadService;

    private SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-HH-dd");

    @ApiOperation(value = "活动累计人气v4.3.0", notes = "活动累计人气")
    @RequestMapping(value = "/count", method = RequestMethod.POST)
    @UnUpdateCheck
    public AjaxResponseBody<Map> url() {
//        Integer renqi5 = jdbcTemplate.queryForObject("select count(distinct(um.user_id)) from user_maker um left join user_invite_relation uir on um.user_id = uir.parent_id where uir.type = 2 and uir.parent_id is not null", Integer.class);
//        Integer renqi1 = jdbcTemplate.queryForObject("select count(distinct(um.user_id)) from user_maker um left join user_invite_relation uir on um.user_id = uir.parent_id where uir.type = 2 and uir.parent_id is null", Integer.class);
//        Integer yongjinrenshu = jdbcTemplate.queryForObject("select count(distinct(user_id)) from user_bill where type =2", Integer.class);
//        Integer yongjinjine = jdbcTemplate.queryForObject("select sum(operator_amount) from user_bill where type =2", Integer.class);
//        Integer renqi = renqi5 * 5 + renqi1;
//        Integer amount = 0;
//        try {
//            amount = yongjinjine / yongjinrenshu;
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        HashMap<String, Object> map = new HashMap<>();
//        map.put("renqi", renqi);
//        map.put("amount", amount);
        Date now = new Date();
        Date begin = DateUtil.beginOfMonth(now);
        Long renqi = DateUtil.between(begin, now, DateUnit.SECOND, true);
        HashMap<String, Object> map = new HashMap<>();
        map.put("renqi", renqi);
        map.put("amount", (30 + (simpleDateFormat.format(new Date()).hashCode() & 7)) * 100);
        return AjaxResponseBody.success(map);
    }

    private static final WeightRandom<Integer> cashOutWeightRandom = new WeightRandom<>(
            new WeightRandom.WeightObj[]{
                    new WeightRandom.WeightObj<>(50, 45.00),
                    new WeightRandom.WeightObj<>(100, 30.00),
                    new WeightRandom.WeightObj<>(500, 20.00),
                    new WeightRandom.WeightObj<>(1000, 5.00)
            }
    );

    @ApiOperation(value = "推手广播v4.4.2", notes = "推手广播")
    @RequestMapping(value = "/board", method = RequestMethod.POST)
    @UnUpdateCheck
    public AjaxResponseBody<ArrayList<String>> board() {
        ArrayList<String> resultList = new ArrayList<>();
        //提现记录
        List<CashOutOrder> cashOutOrderList = cashOutOrderService.list(
                new QueryWrapper<CashOutOrder>()
                        .eq(CashOutOrder.AMOUNT_TYPE, 2)
                        .eq(CashOutOrder.STATUS, 2)
                        .last("order by RAND() limit 100")
        );
        if (CollectionUtil.isNotEmpty(cashOutOrderList)) {
            for (CashOutOrder cashOutOrder : cashOutOrderList) {
                try {
                    resultList.add(String.format(
                            "%s赚佣提现%s.00元！",
                            "恭喜用户" + PhoneUtil.replace(cashOutOrder.getPhone()) + "  ",
                            String.valueOf(cashOutWeightRandom.next())
                    ));
                } catch (Exception e) {
                    log.error("推手广播错误:{}", JSONUtil.toJsonStr(cashOutOrder));
                }
            }
        }
        //推广记录
//        List<Map<String, Object>> tuiGuangList = jdbcTemplate.queryForList("SELECT ui.phone,um.maker_level FROM user_info ui JOIN user_invite_relation uir ON ui.id=uir.parent_id JOIN user_maker um ON uir.user_id=um.user_id ORDER BY RAND() LIMIT 30");
//        if (CollectionUtil.isNotEmpty(tuiGuangList)) {
//            for (Map<String, Object> tuiGuang : tuiGuangList) {
//                try {
//                    Integer makerLevel = Convert.toInt(tuiGuang.get("maker_level"), 1);
//                    String makerLevelStr = "白银推手";
//                    switch (makerLevel) {
//                        case 3:
//                            makerLevelStr = "钻石推手";
//                        case 2:
//                            makerLevelStr = "黄金推手";
//                    }
//                    resultList.add(String.format(
//                            "%s成功邀请好友成为%s！",
//                            PhoneUtil.replace(String.valueOf(tuiGuang.get("phone"))),
//                            makerLevelStr
//                    ));
//                } catch (Exception e) {
//                    log.error("推手广播错误:{}", JSONUtil.toJsonStr(tuiGuang));
//                }
//            }
//        }
        //酒店预订
//        List<Map<String, Object>> hotelYuDingList = jdbcTemplate.queryForList("SELECT ui.phone,oh.snapshot_hotel_name FROM order_head oh JOIN user_info ui ON ui.id=oh.user_id WHERE oh.vip_flag=3 AND oh.`status` IN (2,3,7,9) ORDER BY RAND() LIMIT 30");
//        if (CollectionUtil.isNotEmpty(hotelYuDingList)) {
//            for (Map<String, Object> hotelYuDing : hotelYuDingList) {
//                try {
//                    resultList.add(String.format(
//                            "%s成功预订%s！",
//                            PhoneUtil.replace(String.valueOf(hotelYuDing.get("phone"))),
//                            String.valueOf(hotelYuDing.get("snapshot_hotel_name"))
//                    ));
//                } catch (Exception e) {
//                    log.error("推手广播错误:{}", JSONUtil.toJsonStr(hotelYuDing));
//                }
//            }
//        }
        Collections.shuffle(resultList);
        //
        return AjaxResponseBody.success(resultList);
    }

}
